My Shell
CS 345 - Project One
Purpose
A shell (also called a Command Language Interpreter) is an interface to an Operating System.
A shell is a software module that interprets textual commands coming either from the
user’s keyboard or from a script file and executes the commands either directly or
creates a new child process to execute the command. Signal processing
is generally at the shell level. Usually, a shell will
provide command history and interactive name completion mechanisms.
Project Description
For Project 1, you are to write a shell task for your operating system.
Your shell should prompt the user for keyboard input.
After a sequence of characters is entered followed by a return, the string is parsed
and the indicated command executed, either directly or as a background task.
The following are important guidelines for the shell exercise:
- Create a new project in your IDE. Download and add the following files
to your OS project (by right clicking and choosing "Save Target As..."):
Edit os345.h (if necessary) to select host OS/IDE/ISA. (Only enable
one of the following defines: DOS, GCC, MAC, or NET.)
- Your shell should be written in C and as a task (P1_shellTask) scheduled from
your operating system (os345.c).
(Shell commands are C functions.)
- All commands and parameters should come from the keyboard inbuffer string
or from a script file (Project 6)
and parsed into traditional C argc and malloc'd argv
variables.
- Your shell must support background execution of command programs.
An ampersand (&) at the end of the command line indicates to the shell that
the command is to be executed by a newly created, background task. The
command arguments are passed to the new task in the malloc'd
argv strings for execution.
After creating the new task, the shell then
recovers malloc'd memory and immediately prompts for the
next command.
Otherwise, your shell executes the command directly with a function call,
waiting for the function to return before recovering memory and reprompting
for the next command.
- To simulate character interrupts, keyboard character inputs are to be
polled (pollInterrupts) during the scheduling loop. (SWAP macros should
be liberally placed throughout your user code to ensure timely execution of
your polling routine - DO NOT put SWAP's in system code).)
- Command parameters are strings, quoted strings ("This is one argument"),
decimal numbers, or hexadecimal numbers (ie. 0xa19f).
- Other than the few required commands, you are to decide the look and feel
(syntax and semantics) as well as which commands to implement.
You may define command and argument delimiters, but you must
address case sensitivity issues. (Is "LS" the same as "ls" or "Ls"?)
Commands may be terse and/or verbose (ie, ls and list).
- Some extended form of a help command is required.
- Before a process is scheduled, any pending signals must be properly handled
as explained below.
- Make your shell scalable as you will be adding additional functionality in
subsequent projects.
Signals
A signal is a limited form of inter-process communication used in Unix, Unix-like,
and other POSIX-compliant operating systems.
Essentially, a signal is an asynchronous notification of an event that is
sent to a process before it is rescheduled for execution.
When a signal is sent to a process, the operating system interrupts
the process' normal flow of execution (when it is scheduled) and executes a signal handler.
If the process has previously registered a signal handler, that routine is executed.
Otherwise the default signal handler is executed.
Execution can be interrupted during any non-atomic instruction.
Blocked processes
are not un-blocked by a signal, but rather the signal remains pending until such
time as the process is un-blocked and scheduled for execution.
The following table is summarizes how signals might be handled by your operating system:
| Signal |
PollInterrupts |
Scheduler |
Dispatcher |
Signal Handler |
| SIGCONT |
Cntrl-R
sigSignal(-1, SIGCONT);
(Clear SIGSTOP
and SIGTSTP in all tasks) |
n.a. |
Clear SIGCONT
Call sigContHandler()
Schedule task |
return; |
| SIGINT |
Cntrl-X
sigSignal(0, SIGINT);
semSignal(inBufferReady); |
n.a. |
Clear SIGINT
Call sigIntHandler()
Schedule task |
sigSignal(-1, SIGTERM); |
| SIGTERM |
n.a. |
n.a. |
Clear SIGTERM
Call sigTermHandler()
Do not schedule task |
killTask(curTask);
(Note: killTask does not
terminate shell) |
| SIGTSTP |
Cntrl-W
sigSignal(-1, SIGTSTP); |
n.a. |
Clear SIGTSTP
Call sigTstpHandler()
Do not schedule task |
sigSignal(-1, SIGSTOP); |
| SIGSTOP |
n.a. |
TASK NOT SCHEDULED |
n.a. |
No handler needed |
Handling Control Signals
Certain combinations of characters entered at the controlling terminal
of a running process
cause the system to signal the process. Signals are processed just
before a process is scheduled for execution. For project 1, the following
control character signals are to be handled:
- Cntrl-X sends an INT signal (SIGINT) to the shell process (task 0),
clears the input buffer, and semSignal's the input buffer
ready (inBufferReady);
the shell SIGINT handler should send a TERMINATE signal (SIGTERM) to
all tasks.
- Cntrl-R sends a CONTINUE signal (SIGCONT) to all tasks; clears
SIGSTOP and SIGTSTP signals from all tasks.
- Cntrl-W sends a STOP signal (SIGTSTP) to all tasks.
Signal handlers can be installed with the sigAction() system call.
If a signal handler is not installed for a particular signal, the default
handler is used.
Programming Environments
The choice of the software tools and programming environment is your choice.
The C programming language will be used for all programming assignments and is available as follows:
- Windows users can download a very good C compiler at the LCC website
(http://www.cs.virginia.edu/~lcc-win32/) for free!
- For those wanting to use Linux on their home computers, look at the
KNOPPIX website (http://www.knoppix.net/) for a free Linux system bootable from CD ROM.
- For those who do not own a computer or would just rather do their work using BYU facilities,
there are Windows/Linux/Unix labs available in the Computer Science building.
A gcc compiler is available on all Linux machines.
The EMACS text editor would probably be the editor of choice.
- Change the “os345.h” header file variables DOS, GCC, MAC, or NET to reflect your environment.
- In all cases, it is the student’s responsibility to present their work to a TA for pass-off.
Grading and Pass-off
Your Shell is to be demonstrated in person to a TA as follows:
- Your Shell task should parse the command line arguments into traditional argc and argv C variables.
Use malloc to allocate space for argv and the string arguments.
Recover the allocated space with the free function when a task/function terminates/returns.
argc is the number of command line arguments.
argv is a pointer to an array of pointers that point to the character string arguments.
argv[0] is the first parameter (the command), so argc will always be at least 1.
- Implement the following shell commands:
- Add – add all numbers in command line (decimal or hexadecimal).
- Args – list all parameters on the command line, numbers or strings.
- Date/Time – output current system date and time.
- Help – list all user commands and the syntax of their arguments.
Help should be selective and have at least two levels of hierarchical depth.
- Implement background execution of programs. If the command line ends with an ampersand (&),
your shell should create a new task to execute the command line.
Otherwise, your shell should directly call the command function (and wait for the
function to return.) Use the createTask function to create a background process.
- Add signal functionality to your shell such that:
- Cntrl-X terminates (kills) all tasks except task 0 (shell).
- Cntrl-W pauses the execution of all tasks.
- Cntrl-R continues the execution of all tasks after a pause.
- Implement additional functionality to your Shell such as:
- Command line recall of one or more previous commands. (HIGHLY RECOMMENDED!)
- Help implemented using the more filter.
- Be able to edit command line (insert / delete characters).
- Chain together multiple commands separated by some delimiter.
- Wild cards.
- List / Set command line variables.
- Implement aliasing.
- Calculator – perform basic binary operations.
- Allow for other number bases such as binary (%) and octal (0).
- There are 20 points possible for Project 1. The grading criteria will be as follows:
- 2 pts – Commands and arguments are case insensitive. (Preserve case in
quoted strings.)
- 2 pts – Backspace is implemented (delete character to the left) and correctly handles input buffer under/overflow.
- 2 pts – Your shell parses the command line into argc
and malloc'd argv argument variables. Modify createTask
to again malloc and copy argv argument variables.
All malloc'd memory is appropriately recovered - command line by the shell
and createTask by killTask. (Don't create any memory leaks!
Don't worry at this time about any pre-existing system memory leaks such as with
the reset command.)
- 2 pts – Quoted strings and hexadecimal arguments are implemented.
- 2 pts – An additional shell function of your choice is implemented.
(See above item 5.)
- 2 pts – The four required shell commands (add,
args, date/time, and help) are correctly implemented.
- 4 pts - The signals SIGCONT, SIGINT, SIGTSTP,
SIGTERM, and SIGSTOP function properly as described above.
- 4 pts – Your shell supports background execution of all commands.
- -2 points penalty for each school day late.
- -10 points penalty for NOT implementing delete character (backspace) functionality.
In addition, after completing the above requirements,
the following bonus points may be awarded:
- +2 points bonus for early pass-off (at least one day before due date.)
- +2 points bonus for implementing command line recall.
NOTE: Bonus points may be received anytime during the semester
(regardless of any late penalties) as long as all the project
requirements have been completed.